package com.jinxin.platform.cdacsrecord.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.*;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.ResultEntity;
import com.jinxin.platform.cdacsrecord.core.service.*;
import com.jinxin.platform.cdacsrecord.core.utils.ResultMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenyinlong
 * @Description:
 * @Date: Create in 10:46 2020/4/3
 */
@RestController
@RequestMapping("/accessDevice")
@Slf4j
public class AcsControlDeviceController {
    @Autowired
    AcsControlDeviceInfoService acsControlDeviceInfoService;

    @Autowired
    AcsControlRoomInfoService acsControlRoomInfoService;

    @Autowired
    AcsControlBlockInfoService acsControlBlockInfoService;

    @Autowired
    private AcsControlUserInfoService acsControlUserInfoService;

    @Autowired
    private AcsControlDeviceSubscribeService acsControlDeviceSubscribeService;

    @Autowired
    private AcsControlSysParamService acsControlSysParamService;
    @Autowired
    private AcsControlAsyncService asyncService;
    @Autowired
    private AcsControlModuleInvokeService invokeService;

    @GetMapping("/openDoor")
    public ResultEntity openDoor(String mac){
        String userInfo = invokeService.getLoginUserInfo();
        String openRes = acsControlDeviceInfoService.openDoor(mac);
        if(openRes.equals("开门成功") ){
            asyncService.addReportOpenLog(mac,"手机APP开门",JSONObject.parseObject(userInfo).get("userAccount").toString());
        }
        return new ResultEntity("OK",openRes);
    }

    @GetMapping("/getDevice")
    public ResultEntity getDevice(String type) {
        List<Map> deviceInfos = acsControlDeviceSubscribeService.selAllDevice();
        if (deviceInfos.size() == 0) {
            if (acsControlDeviceInfoService.getDevice()){
                deviceInfos = acsControlDeviceSubscribeService.selAllDevice();
            }else {
                return new ResultEntity("1", "OK", null);
            }
        }
        List<Map> resultMap = new ArrayList<>();
        AcsControlSysParamDO sysParamDO = new AcsControlSysParamDO();
        switch (type) {
            case "outdoor":
                sysParamDO.setParamKey(type);
                List<AcsControlSysParamDO> outdoor = acsControlSysParamService.selParamByInfo(sysParamDO);
                for (AcsControlSysParamDO a :
                        outdoor) {
                    for (Map map :
                            deviceInfos) {
                        if (map.get("DEVICE_NAME").toString().contains(a.getParamVal())) {
                            resultMap.add(map);
                        }
                    }
                }
                break;
            case "entrance":
                sysParamDO.setParamKey(type);
                List<AcsControlSysParamDO> entrance = acsControlSysParamService.selParamByInfo(sysParamDO);
                for (AcsControlSysParamDO a :
                        entrance) {
                    for (Map map :
                            deviceInfos) {
                        if (map.get("DEVICE_NAME").toString().contains(a.getParamVal())) {
                            resultMap.add(map);
                        }
                    }
                }
                break;
            case "indoor":
                sysParamDO.setParamKey(type);
                List<AcsControlSysParamDO> indoor = acsControlSysParamService.selParamByInfo(sysParamDO);
                for (AcsControlSysParamDO a :
                        indoor) {
                    for (Map map :
                            deviceInfos) {
                        if (map.get("DEVICE_NAME").toString().contains(a.getParamVal())) {
                            resultMap.add(map);
                        }
                    }
                }
                break;
        }
        return new ResultEntity("1", "OK", resultMap);
    }

    @GetMapping("/flushDevice")
    public ResultEntity flushDevice(){
        if(acsControlDeviceInfoService.getDevice()){
            return new ResultEntity("1","成功");
        }
        return new ResultEntity("2","失败");
    }

    @GetMapping("/getRoom")
    public ResultEntity getRoom(String buildLoc) {
        List<Map> acsControlRoomInfoDOS = new ArrayList<>();
        if (!StringUtils.isEmpty(buildLoc)) {
            acsControlRoomInfoDOS = acsControlRoomInfoService.selRoomByBuild(buildLoc);
        }
        return new ResultEntity("1", "OK", acsControlRoomInfoDOS);
    }

    @GetMapping("/getPeople")
    public ResultEntity getPeople(String buildLoc) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        JSONObject jsonObject;
        List<AcsControlUserInfoDO> acsControlUserInfoDOS;
        if (!StringUtils.isEmpty(buildLoc)) {
            AcsControlUserInfoDO acsControlUserInfoDO = new AcsControlUserInfoDO();
            acsControlUserInfoDO.setUserBlockName(buildLoc);
            acsControlUserInfoDOS = acsControlUserInfoService.selDOByInfo(acsControlUserInfoDO, "");
        } else {
            acsControlUserInfoDOS = acsControlUserInfoService.selAll();
        }
        for (AcsControlUserInfoDO a :
                acsControlUserInfoDOS) {
            jsonObject = new JSONObject();
            jsonObject.put("name", a.getUserName());
            jsonObject.put("value", a.getUserId());
            jsonObjects.add(jsonObject);
        }
        return new ResultEntity("1", "OK", jsonObjects);
    }

    @GetMapping("/getBuilding")
    public ResultEntity getBuilding(String type) {
        List<Map> acsControlBlockInfoDOS;
        if (!StringUtils.isEmpty(type)){
            acsControlBlockInfoDOS = acsControlBlockInfoService.selAllBlockByState();
        }else {
            acsControlBlockInfoDOS = acsControlBlockInfoService.selAllBlock();
        }
        return new ResultEntity("1", "OK", acsControlBlockInfoDOS);
    }

    @GetMapping("/getTable")
    public Map<String, Object> getTableInfo(int page, int limit, String type, String deviceName, String buildingLoc, String roomLoc) {
        PageInfo<AcsControlDeviceInfo> acsControlDeviceInfos = acsControlDeviceInfoService.queryByLimitAndCondition(page, limit, type, deviceName, buildingLoc, roomLoc);
        return ResultMap.resultMap(acsControlDeviceInfos.getTotal(),acsControlDeviceInfos.getList());
    }

    @PostMapping("/saveDevice")
    public ResultEntity saveDevice(AcsControlDeviceInfo acsControlDeviceInfo) {
        return acsControlDeviceInfoService.saveDevice(acsControlDeviceInfo);
    }

    @GetMapping("/deleteDevice")
    public ResultEntity deleteDevice(String id) {
        AcsControlDeviceInfo acsControlDeviceInfo = acsControlDeviceInfoService.queryById(id);
        if (acsControlDeviceInfo.getType().equals("单元门口机")||acsControlDeviceInfo.getType().equals("门口闸机")){
            acsControlDeviceInfoService.deleteAllFace(acsControlDeviceInfo.getDeviceId());
            if (acsControlDeviceInfo.getType().equals("单元门口机")){
                AcsControlBlockInfoDO acsControlBlockInfoDO = new AcsControlBlockInfoDO();
                acsControlBlockInfoDO.setBlockName(acsControlDeviceInfo.getBuildingLoc());
                List<AcsControlBlockInfoDO> acsControlBlockInfoDOS = acsControlBlockInfoService.selDOByInfo(acsControlBlockInfoDO);
                acsControlBlockInfoDOS.get(0).setState("0");
                acsControlBlockInfoService.updateDO(acsControlBlockInfoDOS.get(0));
            }
        }
        AcsControlDeviceSubscribe acsControlDeviceSubscribe = acsControlDeviceSubscribeService.queryById(acsControlDeviceInfo.getDeviceId());
        acsControlDeviceSubscribe.setIfUser("N");
        acsControlDeviceSubscribeService.update(acsControlDeviceSubscribe);
        acsControlDeviceInfoService.deleteById(id);
        return new ResultEntity("OK", "删除成功!");
    }

    @GetMapping("/getAllDeviceInfo")
    public ResultEntity getTableInfo() {
        List<AcsControlDeviceInfo> acsControlDeviceInfos = acsControlDeviceInfoService.selAll();
        return new ResultEntity("OK", "suc", acsControlDeviceInfos);
    }

    @GetMapping("/updateNumber")
    public ResultEntity updateNumber(String id){

        AcsControlDeviceInfo acsControlDeviceInfo = new AcsControlDeviceInfo();
        acsControlDeviceInfo.setType("物业室内机");
        List<AcsControlDeviceInfo> acsControlDeviceInfos =acsControlDeviceInfoService.selAllByParam(acsControlDeviceInfo);

        

        return new ResultEntity("1","OK");
    }

}
